<template>
  <view class="register-container">
		<view class="register-title">
			<text>注册</text>
		</view>
    <view class="register-box">
      <uni-forms ref="form" :modelValue="formData" :rules="rules">
        <uni-forms-item label="手机号" name="phone">
          <uni-easyinput 
            type="number" 
            v-model="formData.phone"
            placeholder="请输入手机号"
            trim
          />
        </uni-forms-item>
        <uni-forms-item label="验证码" name="code">
          <view class="code-input">
            <uni-easyinput 
              type="number" 
              v-model="formData.code"
              placeholder="请输入验证码"
              trim
            />
            <button 
              class="get-code-btn" 
              :disabled="codeCountdown > 0"
              @click="getCode"
            >
              {{ codeCountdown > 0 ? `${codeCountdown}s后重试` : '获取验证码' }}
            </button>
          </view>
        </uni-forms-item>
        <uni-forms-item label="密码" name="password">
          <uni-easyinput 
            type="password" 
            v-model="formData.password"
            placeholder="请输入密码"
            trim
          />
        </uni-forms-item>
        <uni-forms-item label="确认密码" name="confirmPassword">
          <uni-easyinput 
            type="password" 
            v-model="formData.confirmPassword"
            placeholder="请再次输入密码"
            trim
          />
        </uni-forms-item>
        <uni-popup ref="errorPopup" type="message">
          <uni-popup-message 
            type="error" 
            :message="errorMessage" 
            :duration="2000"
          />
        </uni-popup>
        <button 
          class="register-btn" 
          type="primary" 
          :loading="isLoading"
          @click="submitForm"
        >
          {{ isLoading ? '注册中...' : '立即注册' }}
        </button>
      </uni-forms>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      formData: {
        phone: '',
        code: '',
        password: '',
        confirmPassword: ''
      },
      isLoading: false,
      errorMessage: '',
      codeCountdown: 0,
      rules: {
        phone: {
          rules: [{
            required: true,
            errorMessage: '请输入手机号'
          }, {
            pattern: /^1[3-9]\d{9}$/,
            errorMessage: '手机号格式不正确'
          }]
        },
        code: {
          rules: [{
            required: true,
            errorMessage: '请输入验证码'
          }, {
            length: 6,
            errorMessage: '验证码为6位数字'
          }]
        },
        password: {
          rules: [{
            required: true,
            errorMessage: '请输入密码'
          }, {
            minLength: 6,
            maxLength: 20,
            errorMessage: '密码长度在6到20个字符之间'
          }]
        },
        confirmPassword: {
          rules: [{
            required: true,
            errorMessage: '请确认密码'
          }, {
            validateFunction: (rule, value, data, callback) => {
              if (value !== this.formData.password) {
                callback('两次输入的密码不一致')
              }
              return true
            }
          }]
        }
      }
    }
  },
  methods: {
    async submitForm() {
      try {
        await this.$refs.form.validate()
        this.isLoading = true
        this.errorMessage = ''
        // 模拟注册请求
        const res = await this.mockRegister()
        
        if (res.success) {
          uni.showToast({
            title: '注册成功',
            icon: 'success'
          })
          setTimeout(() => {
            uni.navigateBack()
          }, 1500)
        } else {
          this.errorMessage = res.message
          this.$refs.errorPopup.open()
        }
      } catch (err) {
        this.errorMessage = err.message
        this.$refs.errorPopup.open()
      } finally {
        this.isLoading = false
      }
    },
    mockRegister() {
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({ success: true })
        }, 1000)
      })
    },
    getCode() {
      if (this.codeCountdown > 0) return
      
      this.codeCountdown = 60
      const timer = setInterval(() => {
        this.codeCountdown--
        if (this.codeCountdown <= 0) {
          clearInterval(timer)
        }
      }, 1000)
			console.log("验证码发送中...")
    }
  }
}
</script>

<style lang="scss" scoped>
.register-container {
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: radial-gradient(
    circle at 50% 0%,
    rgba(100,181,246,0.1) 0%,
    rgba(255,255,255,1) 100%
  );
  position: relative;
  overflow: hidden;
}

.register-box {
  width: 90%;
  padding: 40rpx;
  height: auto;
  min-height: 400rpx;
  background: linear-gradient(
    145deg,
    rgba(255, 255, 255, 0.95) 0%,
    rgba(255, 255, 255, 0.98) 100%
  );
  border-radius: 32px;
  box-shadow: 
    0 4px 24px rgba(100,181,246,0.1),
    0 8px 48px rgba(100,181,246,0.05);
  position: relative;
  z-index: 2;
  backdrop-filter: blur(16px);
  animation: fadeInUp 0.8s ease;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 32px;
    background: radial-gradient(
      circle at 50% 0%,
      rgba(100,181,246,0.05) 0%,
      transparent 80%
    );
    z-index: -1;
    opacity: 0.6;
    animation: float 6s ease-in-out infinite;
  }
  
  &:hover {
    transform: translateY(-4px);
    box-shadow: 
      0 8px 32px rgba(100,181,246,0.15),
      0 12px 64px rgba(100,181,246,0.08);
    border-color: rgba(100,181,246,0.12);
    
    &::before {
      opacity: 0.8;
    }
  }
}

.register-title {
  padding: 30px;
  height: 100rpx;
  text-align: center;
  font-size: 60rpx;
  font-weight: bold;
  color: #1f92f099;
  margin: 60rpx 0 40rpx;
  text-shadow: 0 4px 8px rgba(100,181,246,0.15);
  animation: fadeInUp 0.8s ease;
}

.code-input {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.get-code-btn {
  white-space: nowrap;
  padding: 0 20rpx;
  height: 70rpx;
  line-height: 70rpx;
  font-size: 28rpx;
  background: linear-gradient(145deg, #65c9ed8b 0%, #c6def1c0 100%);
  border: none;
  box-shadow: 0 4px 16px rgba(100,181,246,0.2);
  border-radius: 20rpx;
  font-weight: 600;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 24px rgba(100,181,246,0.4);
  }
  
  &:active {
    transform: scale(0.96);
    box-shadow: 0 2px 8px rgba(100,181,246,0.2);
  }
  
  &:disabled {
    opacity: 0.7;
    cursor: not-allowed;
  }
}

.register-btn {
  margin-top: 40rpx;
  position: relative;
  overflow: hidden;
  background: linear-gradient(145deg, #65c9ed8b 0%, #c6def1c0 100%);
  border: none;
  box-shadow: 0 4px 16px rgba(100,181,246,0.2);
  border-radius: 20rpx;
  font-weight: 600;
  font-size: 36rpx;
  line-height: 60rpx;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  transform-style: preserve-3d;
  color: #fff;
  text-shadow: 0 2px 4px rgba(0,0,0,0.1);
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
      90deg,
      transparent,
      rgba(255,255,255,0.3),
      transparent
    );
    transition: all 0.5s ease;
  }
  
  &::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle, rgba(255,255,255,0.3) 10%, transparent 10.01%);
    transform: scale(0);
    transition: transform 0.5s, opacity 0.5s;
    pointer-events: none;
  }
  
  &:hover {
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 6px 24px rgba(100,181,246,0.4);
    background: linear-gradient(145deg, #90caf9 0%, #64b5f6 100%);
    
    &::before {
      left: 100%;
    }
  }
  
  &:active {
    transform: scale(0.96);
    box-shadow: 0 2px 8px rgba(100,181,246,0.2);
    
    &::after {
      transform: scale(2);
      opacity: 0;
    }
  }
  
  &.loading {
    &::before {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      width: 24rpx;
      height: 24rpx;
      border: 3rpx solid #fff;
      border-top-color: transparent;
      border-radius: 50%;
      animation: spin 0.8s linear infinite;
      transform: translate(-50%, -50%);
    }
    
    &::after {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: radial-gradient(circle, rgba(255,255,255,0.2) 10%, transparent 10.01%);
      animation: ripple 1.2s infinite;
    }
  }
}

::v-deep {
.uni-forms-item__label {
  padding: 0 !important;
  color: rgba(100,181,246,0.95);
  font-size: 30rpx;
  font-weight: 600;
  margin-bottom: 16rpx;
  letter-spacing: 0.5rpx;
  text-shadow: 0 2px 4px rgba(100,181,246,0.1);
  transition: all 0.3s ease;
}

.uni-forms-item__label:hover {
  color: #64b5f6;
  text-shadow: 0 4px 8px rgba(100,181,246,0.2);
}

.uni-forms-item__content {
  margin-top: 20rpx;
}

::v-deep .uni-easyinput__content {
  border: 1px solid rgba(100,181,246,0.15);
  border-radius: 20rpx;
  transition: all 0.3s ease;
  box-shadow: 
    0 2px 6px rgba(100,181,246,0.05),
    inset 0 1px 2px rgba(255,255,255,0.1);
  background: linear-gradient(
    145deg,
    rgba(255,255,255,0.96) 0%,
    rgba(255,255,255,0.98) 100%
  );
  backdrop-filter: blur(16px);
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
      90deg,
      transparent,
      rgba(100,181,246,0.08),
      transparent
    );
    transition: all 0.6s ease;
  }

  &::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 2rpx;
    background: linear-gradient(90deg, #64b5f6, #90caf9);
    transform: scaleX(0);
    transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  }
  
  &:hover {
    border-color: rgba(100,181,246,0.25);
    box-shadow: 
      0 4px 12px rgba(100,181,246,0.08),
      inset 0 1px 2px rgba(255,255,255,0.15);
    
    &::before {
      left: 100%;
    }
  }
  
  &:focus-within {
    border-color: rgba(100,181,246,0.4);
    box-shadow: 
      0 6px 16px rgba(100,181,246,0.12),
      inset 0 1px 2px rgba(255,255,255,0.2);
    animation: inputFocus 0.3s ease;
    
    &::after {
      transform: scaleX(1);
      background: linear-gradient(90deg, #90caf9, #64b5f6);
    }
  }
}

@keyframes inputFocus {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.02);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes inputLine {
  0% {
    transform: scaleX(0);
  }
  100% {
    transform: scaleX(1);
  }
}
}
</style>
